import 'package:flutter/material.dart';

class VideoEstimatedTimeBarStyle {
  final AlignmentGeometry alignment;
  final Decoration decoration;
  final EdgeInsetsGeometry padding;
  final TextStyle textStyle;

  VideoEstimatedTimeBarStyle({
    this.alignment = Alignment.center,
    this.decoration = const BoxDecoration(
      color: Color(0x7F000000),
      borderRadius: BorderRadius.all(Radius.circular(4.0)),
    ),
    this.padding = const EdgeInsets.all(8.0),
    this.textStyle = const TextStyle(color: Colors.white),
  });
}

class VideoEstimatedTimeBar extends StatefulWidget {
  VideoEstimatedTimeBar({
    Key? key,
    required this.notifier,
    VideoEstimatedTimeBarStyle? videoEstimatedTimeBarStyle,
  })  : videoEstimatedTimeBarStyle =
            videoEstimatedTimeBarStyle ?? VideoEstimatedTimeBarStyle(),
        super(key: key);

  final EstimatedTimeNotifier notifier;
  final VideoEstimatedTimeBarStyle videoEstimatedTimeBarStyle;

  @override
  State<VideoEstimatedTimeBar> createState() => _VideoEstimatedTimeBarState();
}

class _VideoEstimatedTimeBarState extends State<VideoEstimatedTimeBar> {
  EstimatedTimeNotifier get notifier => widget.notifier;

  VideoEstimatedTimeBarStyle get videoEstimatedTimeBarStyle =>
      widget.videoEstimatedTimeBarStyle;

  String _position = '--:--';
  String _duration = '--:--';
  bool _hidden = true;

  @override
  void initState() {
    super.initState();
    notifier.addListener(_listener);
  }

  @override
  Widget build(BuildContext context) {
    return Offstage(
      offstage: _hidden,
      child: Align(
        alignment: videoEstimatedTimeBarStyle.alignment,
        child: Container(
          decoration: videoEstimatedTimeBarStyle.decoration,
          padding: videoEstimatedTimeBarStyle.padding,
          child: Text(
            '$_position / $_duration',
            style: const TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  void _listener() {
    if (notifier.duration.inHours == 0) {
      var strPosition = notifier.position.toString().split('.')[0];
      var strDuration = notifier.duration.toString().split('.')[0];
      _position = "${strPosition.split(':')[1]}:${strPosition.split(':')[2]}";
      _duration = "${strDuration.split(':')[1]}:${strDuration.split(':')[2]}";
    } else {
      _position = notifier.position.toString().split('.')[0];
      _duration = notifier.duration.toString().split('.')[0];
    }
    _hidden = notifier.isEnd;
    setState(() {});
  }

  @override
  void dispose() {
    notifier.removeListener(_listener);
    super.dispose();
  }
}

class EstimatedTimeNotifier with ChangeNotifier {
  Duration _position = const Duration(seconds: 0);
  Duration _duration = const Duration(seconds: 0);
  bool _isEnd = true;

  Duration get position => _position;

  Duration get duration => _duration;

  bool get isEnd => _isEnd;

  void update({required Duration position, required Duration duration}) {
    _position = position;
    _duration = duration;
    _isEnd = false;
    notifyListeners();
  }

  void dragEnd() {
    _isEnd = true;
    notifyListeners();
  }
}
